﻿// See https://aka.ms/new-console-template for more information

string applicationName = System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name;
string executable = Environment.ProcessPath!.Replace(".vshost", "");

// 没有参数删除的方法
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string path = AppDomain.CurrentDomain.BaseDirectory;
if (desktop + "\\" == path)
{
    Console.WriteLine("防止误删，无法删除桌面文件(ENTER键退出)........");
    Console.ReadLine();
    return;
}


// 选中文件夹删除
string filename;
List<string> selected = new();
foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
{
    filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
    if (filename.ToLowerInvariant() == "explorer" || filename.ToLowerInvariant() == "资源管理器")
    {
        Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
        foreach (Shell32.FolderItem item in items)
        {
            if (item.Path == executable)
            {
                continue;
            }
            selected.Add(item.Path);
            //Console.WriteLine(item.Path);

        }
    }
}


if (selected.Count > 0)
{
    foreach (var item in selected)
    {
        Directory.Delete(item, true);
    }

}
// 参数删除的方法
else if (args.Length > 0 && Directory.Exists(args[0]))
{
    Directory.Delete(args[0], true);

}
else
{
    try
    {
        Console.WriteLine("正在删除中............");
        DirectoryInfo directoryInfo = new(path);
        var fileList = directoryInfo.GetFileSystemInfos();
        foreach (var file in fileList)
        {
            if (file.Name == applicationName || file.FullName == executable)
            {
                continue;
            }
            if (Directory.Exists(file.FullName))
            {
                Directory.Delete(file.FullName, true);
            }
            else
            {
                file.Delete();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.ReadKey();
    }
}













